package models

import (
	"github.com/jinzhu/gorm"
	"strconv"
	"time"
)

const (
	NormalUser  = 1
	AdminUser   = 3
	ManagerUser = 2
)

type User struct {
	ID       int    `gorm:"primary_key" json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
	Phone    string `json:"phone"`
	Email    string `json:"email"`
	LoginOn  string `json:"loginOn"`
	Role     int    `json:"role"`
	Name     string `json:"name"`
	Class    string `json:"class"`
}

// CheckLogin checks if authentication information exists
func CheckLogin(username, password string) (int, error) {
	var auth User
	err := db.Select("id").Where(User{Username: username, Password: password}).First(&auth).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return auth.ID, nil
}

func GetUserByID(id int) (User, error) {
	var user User
	err := db.Where("id = ?", id).First(&user).Error
	return user, err
}

// ExistUserByPhone check user exist by phone
func ExistUserByUsername(username string) (bool, error) {
	var user User
	err := db.Select("id").Where("username = ? AND deleted_on = ?", username, 0).Find(&user).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}
	return user.ID > 0, nil
}

func ExistUserByID(id int) (bool, error) {
	var user User
	err := db.Select("id").Where("id = ? AND deleted_on = ?", id, 0).Find(&user).Error
	if err != nil && err != gorm.ErrRecordNotFound {
		return false, err
	}
	return user.ID > 0, nil
}

func AddUser(data map[string]interface{}) (int, error) {
	user := User{
		Username: data["username"].(string),
		Password: data["password"].(string),
		//Phone:    data["phone"].(string),
		//Email:    data["email"].(string),
		//Name:     data["name"].(string),
		//Class:    data["class"].(string),
		Role: NormalUser,
	}
	if err := db.Create(&user).Error; err != nil {
		return 0, err
	}
	var u User
	db.Select("id").Where("username = ?", data["username"].(string)).First(&u)
	return u.ID, nil
}

func GetUsers(pageNum, pageSize int, maps interface{}) ([]User, error) {
	var (
		users []User = nil
		err   error  = nil
	)

	if pageSize > 0 && pageNum > 0 {
		err = db.Where(maps).Find(&users).Offset(pageNum).Limit(pageSize).Error
	} else {
		err = db.Where(maps).Find(&users).Error
	}

	if err != nil && err != gorm.ErrRecordNotFound {
		return nil, err
	}
	return users, nil
}

func GetUserTotal(maps interface{}) (int, error) {
	var count int
	if err := db.Model(&User{}).Where(maps).Count(&count).Error; err != nil {
		return 0, err
	}

	return count, nil
}

func DeleteUser(id int) error {
	return db.Where("id = ?", id).Update("deleted_on", 1).Error
	//if err := db.Where("id = ?", id).Delete(&User{}).Error; err != nil {
	//	return err
	//}
	//return nil
}

func EditUser(id int, data interface{}) error {
	err := db.Model(&User{}).Where("id = ? AND deleted_on = ?", id, 0).Updates(data).Error
	return err
}

func UpdateLogin(id int) error {
	now := time.Now().Unix()
	return db.Model(&User{ID: id}).Update("loginOn", strconv.FormatInt(now, 10)).Error
}
